<?php
	require_once('includes/bootstrap.php');
	
	$nom = '';
	$prenom = '';
	$adresse = '';
	$telephone = '';
	$email = '';
	$mdp = '';
	
	if (!empty($_POST))
	{
		$nom = filter_input(INPUT_POST, 'nom', FILTER_SANITIZE_STRING);
		$prenom = filter_input(INPUT_POST, 'prenom', FILTER_SANITIZE_STRING);
		$adresse = filter_input(INPUT_POST, 'adresse', FILTER_SANITIZE_STRING);
		$telephone = filter_input(INPUT_POST, 'telephone', FILTER_SANITIZE_STRING);
		$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
		$mdp = filter_input(INPUT_POST, 'mdp', FILTER_SANITIZE_STRING);
	
		// Check again that all the fields have been filled
		if (empty($nom) || empty($prenom) || empty($adresse) || empty($telephone) || empty($email) || empty($mdp))
		{
			$_SESSION['incomplete_profile'] = true;
		}
		else
		{
			mysql_query(" INSERT INTO clients(name, firstname, email, password, address, phone_number)
										VALUES ('" . mysql_real_escape_string($nom) . "', 
										'" . mysql_real_escape_string($prenom) . "', 
										'" . mysql_real_escape_string($email) . "', 
										'" . mysql_real_escape_string($mdp) . "', 
										'" . mysql_real_escape_string($adresse) . "', 
										'" . mysql_real_escape_string($telephone) . "');");
										
			
			$_SESSION['client']= array(
				'email' => $email,
				'name' => $nom,
				'firstname' => $prenom,
				'password' => $mdp,
				'address' => $adresse,
				'phone_number' => $telephone,
				'id' => mysql_insert_id()
			);
			
			$_SESSION['notification'] = 'Inscription effectuée avec succès.';
			
			if (!empty($_SESSION['commande_validée']))
				header('location: ' . APPLICATION_URL . 'confirm_order.php');
			else
				header('location: ' . APPLICATION_URL);
				
			exit;
		}
		
	}
?>

<html>
	<head>
		<?php include_once('includes/head.php'); ?>
	</head>

	<body>

		<?php include_once('includes/header.php'); ?>
		
		<h2>Inscription</h2>
		
		<script type="text/javascript">
		
			<?php if (!empty($_SESSION['incomplete_profile'])) { ?>
				noty({text: "Merci de bien remplir tous les champs de votre profil : Nom, Prénom, Adresse et Numéro de téléphone.", layout: 'topCenter', type: 'success'});
			<?php unset($_SESSION['incomplete_profile']); ?>
			<?php } ?>
		
		</script>
		
		<form method="POST">
			
			<table>
				<tr>
					<td>Email</td>
					<td><input type="text" name="email" value="<?= $email ?>" /></td>
				</tr>
				<tr>
					<td>Mot de passe</td>
					<td><input type="password" name="mdp" value="<?= $mdp ?>" /></td>
				</tr>
				<tr>
					<td>Nom</td>
					<td><input type="text" name="nom" value="<?= $nom ?>" /></td>
				</tr>
				<tr>
					<td>Prénom</td>
					<td><input type="text" name="prenom" value="<?= $prenom ?>" /></td>
				</tr>
				<tr>
					<td>Adresse</td>
					<td><textarea name="adresse" rows="4"><?= $adresse ?></textarea></td>
				</tr>
				<tr>
					<td>Téléphone</td>
					<td><input type="text" name="telephone" value="<?= $telephone ?>" /></td>
				</tr>
				<tr>
					<td colspan="2" style="text-align: right"><button type="submit" id="Submit">M'inscrire</button></td>
				</tr>
			</table>
			
		</form>
		
		<script type="text/javascript">
			$("input:text, input:password, textarea").addClass("ui-corner-all");
			
			$('form table').addClass('ui-widget ui-widget-content ui-corner-all');

			$("button").button();
		</script>

	</body>

</html>
